var app = getApp(), dataApi = require("../../provider/dataApi.js"), pipe = require("../../provider/pipe.js");

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        list: [],
        page: 1,
        pages: "",
        isInit: !1,
        playIndex: "",
        isDone: !0,
        isLoading: !0,
        ltList: [],
        ltIndex: 0,
        rtList: [],
        rtIndex: 0,
        total: 0,
        psize: 0,
        isTitleType: !0
    },
    ltBindPickerChange: function(event) {
        var self = this;
        this.setData({
            ltIndex: event.detail.value,
            page: 1
        });
        var self = this;
        this.data.ltList.forEach(function(item) {
            item.id == self.data.ltList[event.detail.value].id && self.setData({
                rtList: self.data.ltList[event.detail.value].son,
                rtIndex: 0
            });
        }), this.article(this.data.ltList[event.detail.value].id, this.data.rtList[this.data.rtIndex].id, !1);
    },
    rtBindPickerChange: function(event) {
        this.setData({
            rtIndex: event.detail.value,
            page: 1
        }), this.article(this.data.ltList[this.data.ltIndex].id, this.data.rtList[event.detail.value].id, !1);
    },
    onLoad: function(options) {
        this.setData({
            statusBarHeight: app.globalData.statusBarHeight,
            titleBarHeight: app.globalData.titleBarHeight
        });
    },
    onShow: function() {
        this.getUserInfo(), this.atype();
    },
    goArticle: function(event) {
        app.globalData.id = event.currentTarget.dataset.id, wx.navigateTo({
            url: "../article-info/article-info?type=article&title=" + event.currentTarget.dataset.title
        });
    },
    getUserInfo: function() {
        dataApi.userinfo({
            uid: wx.getStorageSync("uid"),
            op: "getinfo"
        }).then(function(response) {
            console.log(response), wx.setStorageSync("userinfo", response.data.info), wx.setStorageSync("http", response.data.http);
        }).catch(function(error) {
            console.log(error);
        });
    },
    bindPlay: function(event) {
        if (console.log(event), event = event.currentTarget.id, this.data.playIndex) {
            var videoContext = wx.createVideoContext([ "index", this.data.playIndex ].join(""));
            videoContext.seek(0), videoContext.pause();
        }
        this.setData({
            playIndex: event
        }), wx.createVideoContext([ "index", this.data.playIndex ].join("")).play(), this.readVideo(event);
    },
    onHide: function() {
        console.log("隐藏"), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
    },
    onUnload: function() {
        console.log("卸载"), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
    },
    onPullDownRefresh: function() {
        var self = this;
        this.setData({
            page: 1
        }), dataApi.article({
            page: this.data.page,
            pcate: this.data.ltList[this.data.ltIndex].id,
            ccate: this.data.rtList[this.data.rtIndex].id
        }).then(function(response) {
            console.log(response), response.data.list && response.data.list.forEach(function(item) {
                item.time = pipe.formatDateTimehour(item.createtime), item.readnum = pipe.readingvolume(item.readnum);
            }), self.setData({
                list: response.data.list
            }), wx.stopPullDownRefresh();
        }).catch(function(error) {
            wx.stopPullDownRefresh(), console.log(error);
        });
    },
    onReachBottom: function() {
        var self = this;
        this.data.page <= Math.ceil(this.data.total / this.data.psize) && this.setData({
            page: this.data.page + 1,
            isInit: !0
        }), dataApi.article({
            page: this.data.page,
            pcate: this.data.ltList[this.data.ltIndex].id,
            ccate: this.data.rtList[this.data.rtIndex].id
        }).then(function(response) {
            if (console.log(response), response.data.list) {
                response.data.list.forEach(function(item) {
                    item.time = pipe.formatDateTimehour(item.createtime), item.readnum = pipe.readingvolume(item.readnum);
                });
                var combinedList = (combinedList = self.data.list).concat(response.data.list);
                self.setData({
                    list: combinedList,
                    total: response.data.total,
                    psize: response.data.psize
                });
            }
            self.setData({
                isInit: !1
            }), wx.stopPullDownRefresh();
        }).catch(function(error) {
            self.setData({
                isInit: !1
            }), wx.stopPullDownRefresh(), console.log(error);
        });
    },
    article: function(parentCategoryId, childCategoryId, shouldShowLoading) {
        var self = this;
        shouldShowLoading && this.setData({
            isLoading: !0
        }), dataApi.article({
            page: 1,
            pcate: parentCategoryId,
            ccate: childCategoryId
        }).then(function(response) {
            console.log(response), response.data.list && response.data.list.forEach(function(item) {
                item.time = pipe.formatDateTimehour(item.createtime), item.readnum = pipe.readingvolume(item.readnum);
            }), self.setData({
                list: response.data.list,
                pages: Math.ceil(response.data.total / response.data.psize),
                isLoading: !1,
                total: response.data.total,
                psize: response.data.psize
            }), response.data.list.length <= 0 || !response.data ? self.setData({
                isDone: !0,
                isLoading: !1
            }) : self.setData({
                isDone: !1,
                isLoading: !1
            });
        }).catch(function(error) {
            self.setData({
                isLoading: !1
            }), console.log(error);
        });
    },
    readVideo: function(videoId) {
        dataApi.readVideo({
            uid: wx.getStorageSync("uid"),
            id: videoId
        }).then(function(response) {}).catch(function(error) {
            console.log(error);
        });
    },
    atype: function() {
        var self = this;
        dataApi.atype({}).then(function(response) {
            console.log(response), self.setData({
                ltList: response.data.list,
                rtList: response.data.list[0].son
            }), self.article(response.data.list[0].id, response.data.list[0].son[0].id, !0);
        }).catch(function(error) {
            console.log(error), 1 == error.errno && self.setData({
                isTitleType: !1,
                isLoading: !1,
                isDone: !0
            });
        });
    },
    onShareAppMessage: function() {}
});